/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.mam.rbac.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.mam.model.rbac.domain.Operation;
import com.foreveross.bsl.mam.rbac.application.OperationApplication;
import com.foreveross.bsl.mam.rbac.application.vo.OperationVO;

/**
 * <BR>
 * [功能详细描述]
 * @author Amberlo
 * @version [mam-application-impl , 2013-6-23] 
 */
@Named("operationApplication")
public class OperationApplicationImpl implements OperationApplication {

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public OperationVO saveEntity(OperationVO entity) {
		Operation operation = new Operation();
		BeanHelper.copyProperties(operation, entity);
		operation.save();
		entity.setId(operation.getId());
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(OperationVO entity) {
		Operation operation = new Operation();
		BeanHelper.copyProperties(operation, entity);
		operation.delete();
	}
 
	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public OperationVO getEntity(String id) {
		Operation  operation = new Operation ();
		operation = (Operation)operation.findOne(id);
		OperationVO vo = new OperationVO();
		BeanHelper.copyProperties(vo, operation);
		return vo;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<OperationVO> findAll() {
		Operation  operation = new Operation();
		Iterable iterable = operation.findAll();
		if(iterable != null){
			List<OperationVO> result = new ArrayList<OperationVO>();
			OperationVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				Operation  obj = (Operation ) iterator.next();

				vo = new OperationVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable(org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<OperationVO> findByPageable(Pageable pageable) {
		Operation  databaseInfo = new Operation ();
		Page page = databaseInfo.findAll(pageable);
		if(page != null && page.getContent() != null){
			List<OperationVO> result = new ArrayList<OperationVO>();
			OperationVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				Operation  obj = (Operation) iterator.next();

				vo = new OperationVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<OperationVO> pageVO = new PageImpl<OperationVO>(result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}

	@Override
	public OperationVO updateEntity(OperationVO entity) {
		Operation  databaseInfo = new Operation();
		BeanHelper.copyProperties(databaseInfo, entity);
		databaseInfo.save();
		BeanHelper.copyProperties(entity, databaseInfo);
		return null;
	}

	@Override
	public OperationVO getOperationByName(String operationName){
		Operation operation = new Operation();
		Query query = Query.query(Criteria.where("name").is(operationName));
		List<Operation> os = operation.findListByQuery(query);
		if(os==null)return null;
		if(os.size()==0)return null;
		operation = os.get(0);
		OperationVO operationVO = new OperationVO();
		BeanHelper.copyProperties(operationVO, operation);
		return operationVO;
	}

	@Override
	public List<OperationVO> getOperations(List<String> ids) {
		Operation  operation = new Operation();
		Iterable iterable = operation.findAll(ids);
		if(iterable != null){
			List<OperationVO> result = new ArrayList<OperationVO>();
			OperationVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				Operation  obj = (Operation ) iterator.next();

				vo = new OperationVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
			
	}

}
